package com.learn.ssm.chapter2.interceptor;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class InterceptorJdkProxy implements InvocationHandler {

    private  Object target;
    private String interceptorClass = null;

    public InterceptorJdkProxy(Object proxy,String interceptorClass){
        this.target = target;
        this.interceptorClass = interceptorClass;
    }

    public static  Object bind(Object target,String interceptorClass){
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                                      target.getClass().getInterfaces(),
                                      new InterceptorJdkProxy(target,interceptorClass));
    }

    public Object invoke(Object proxy, Method method,Object[] args) throws Throwable{
        if (interceptorClass == null){
            return method.invoke(target,args);
        }

        Object result = null;

        Interceptor interceptor = (Interceptor)Class.forName(interceptorClass).newInstance();

        if(interceptor.before(proxy,target,method,args)){
            result = method.invoke(target,args);
        }else {
            interceptor.around(proxy,target,method,args);
        }

        interceptor.after(proxy,target,method,args);
        return  result;

    }
    public static void main(String [] args){
        HelloWorld proxy1 = (HelloWorld) InterceptorJdkProxy.bind(new HelloWorldImpl(),"com.learn.ssm.chapter2.interceptor.Interceptor1");
        HelloWorld proxy2 = (HelloWorld) InterceptorJdkProxy.bind(proxy1,"com.learn.ssm.chapter2.interceptor.Interceptor2");
        HelloWorld proxy3 = (HelloWorld) InterceptorJdkProxy.bind(proxy2,"com.learn.ssm.chapter2.interceptor.Interceptor3");
        proxy3.sayHelloWorld();
    }
}
